import {p} from './ajax.js'
var ul = document.getElementById("iot-ul2");
var div = document.getElementById("iot-div-search");

function searchdetail(){
var str = document.getElementById("iot-input1").value;
if(str.length==0){return}
p("https://api-hmugo-web.itheima.net/api/public/v1/goods/qsearch?query="+str).then((res)=>{
    //console.log(res.message);
    var a = res.message;
    ul.innerHTML = ""
    ul.width = document.getElementById("iot-input1").width;
    for(var i=0;i<res.message.length;i++){
        let ii =i;
        var li = document.createElement("li");
        ul.appendChild(li);
        li.innerHTML = res.message[i].goods_name;
        li.style.listStyleType = "none"
        li.style.borderBottom = "solid 1px";
        li.style.opacity = "0.8";
        li.style.lineHeight = "300%";
        li.style.textOverflow = "ellipsis";//超出部分以省略号显现
        li.style.overflow = "hidden";//超出文本隐藏
        li.style.whiteSpace = "nowrap";//文本不换行

        li.setAttribute("id","iot-li"+i);
        document.getElementById("iot-li"+i).addEventListener("click",detail);
        function detail(){
            var goods_id = res.message[ii].goods_id;
            window.open("商品详情.html?goods_id="+goods_id,"_self")//传参goods_id,_self此窗口打开页面
            // p("https://api-hmugo-web.itheima.net/api/public/v1/goods/detail?goods_id="+goods_id).then((res)=>{
            //     console.log(res.message);
            //     console.log(ii);
            // })
        }      
 }
})
}

// function searchlist(){
//   p("https://api-hmugo-web.itheima.net/api/public/v1/goods/search?query=曲面电视").then((res)=>{
//      console.log(res.message);
//   })
// }

// searchlist()


function debounce(fn, wait) {
    let timeout;//计时器
    return function () {
      if (timeout) clearTimeout(timeout);
      timeout = setTimeout(fn, wait);
    };
  }
  document.getElementById("iot-input1").oninput = debounce(searchdetail, 1000);//防抖



